﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO;
using System.IO.IsolatedStorage;

namespace JetLag2011
{
    public class TopTenList
    {
        private List<TopTenEntry> entries = new List<TopTenEntry>();
        public int Count
        {
            get
            {
                return entries.Count;
            }
        }
        public ITopTenEntry this[int index]
        {
            get
            {
                if (index >= 0 && index < entries.Count)
                {
                    return entries[index];
                }
                else
                {
                    return null;
                }
            }
        }
        public void ClearMarks()
        {
            foreach(var entry in entries)
            {
                entry.Marked = false;
            }
        }
        private int CompareEntries(TopTenEntry first, TopTenEntry second)
        {
            if(first==null||second==null) throw new NullReferenceException();
            return first.Score-second.Score;
        }
        public void AddEntry(TopTenEntry entry)
        {
            ClearMarks();
            entry.Marked = true;
            entries.Add(entry);
            entries.Sort(CompareEntries);
            while (entries.Count > Constants.TopTen.EntryCount)
            {
                entries.RemoveAt(entries.Count - 1);
            }
        }
        private void FillIn()
        {
            while (Count < Constants.TopTen.EntryCount)
            {
                entries.Add(new TopTenEntry());
            }
        }
        public TopTenList()
        {
            FillIn();
        }
        public TopTenList(XElement fromElement)
        {
            FromXElement(fromElement);
        }
        private void FromXElement(XElement fromElement)
        {
            foreach (var element in fromElement.Elements(Names.XElements.List))
            {
                entries.Add(new TopTenEntry(element));
            }
            FillIn();
        }
        public XElement ToXElement(XName name)
        {
            XElement result = new XElement(name);
            foreach (var entry in entries)
            {
                result.Add(entry.ToXElement(Names.XElements.Entry));
            }
            return result;
        }
    }
    public static class TopTenManager
    {
        private static TopTenList topTen;
        static TopTenManager()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(Names.IsolatedStorage.TopTenFile))
                {
                    IsolatedStorageFileStream stream = store.OpenFile(Names.IsolatedStorage.TopTenFile, FileMode.Open);
                    XDocument doc = XDocument.Load(stream);
                    topTen = new TopTenList(doc.Element(Names.XElements.List));
                    stream.Close();
                }
                else
                {
                    topTen = new TopTenList();
                }
            }
            
        }
        public static TopTenList TopTen
        {
            get
            {
                return topTen;
            }
        }
        public static void Save()
        {
            XDocument doc = new XDocument(topTen.ToXElement(Names.XElements.List));
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream stream = store.OpenFile(Names.IsolatedStorage.TopTenFile, FileMode.Create);
                doc.Save(stream);
                stream.Close();
            }
        }
    }
}
